﻿using System;
using System.IO;

namespace WatiN.Samples.Console
{
    public class TraceLog
    {
        public static string  log_name = "log.txt";
        public static bool bWriteToFile = false;

        private static void writeLog(string str)
        {
            if (!bWriteToFile) return;
            if (str.Length == 0) return;

            str = str.Replace("\b", "");
            StreamWriter sw = new StreamWriter(log_name, true);
            sw.Write(str);
            sw.Flush();
            sw.Close();
        }

        public static void Print(string format, params object[] args)
        {
            string msg = GetMessage(format, args);
            DateTime dt = System.DateTime.Now.ToLocalTime();

            ConsoleColor color = System.Console.ForegroundColor;
            System.Console.ForegroundColor = ConsoleColor.Gray;
            if (_newLine)
            {
                string str = string.Format("[{0}]: ", dt);
                System.Console.Write(str);
                writeLog(str);
                _newLine = false;
            }
            System.Console.ForegroundColor = ConsoleColor.Green;
            System.Console.Write(msg);
            writeLog(msg);
            System.Console.ForegroundColor = color;
        }

        public static void PrintLn(string format, params object[] args)
        {
            Print(format, args);
            writeLog("\r\n");
            System.Console.WriteLine();
            _newLine = true;
        }

        public static void Error(string format, params object[] args)
        {
            string msg = GetMessage(format, args);
            DateTime dt = System.DateTime.Now.ToLocalTime();

            ConsoleColor color = System.Console.ForegroundColor;
            System.Console.ForegroundColor = ConsoleColor.Red;
            if (!_newLine)
            {
                writeLog("\r\n");
                System.Console.WriteLine();
            }
            string str = string.Format("[{0}]: {1}", dt, msg);
            writeLog(str); writeLog("\r\n");
            System.Console.WriteLine(str);
            System.Console.ForegroundColor = color;
            _newLine = true;
        }

        private static string GetMessage(string format, object[] args)
        {
            if (args.Length > 0)
            {
                return string.Format(format, args);
            }
            else
            {
                return format;
            }
        }

        private static bool _newLine = true;
    }
}
